/* This file is part of xREL
 *
 * Copyright (C) 2012 Timo Zimmermann <xrel@timozimmermann.de>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public License
 * along with this library; see the file COPYING.LIB.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

import QtQuick 1.1
import com.nokia.meego 1.0
import com.nokia.extras 1.1

import "delegates"
import "components"
import "OAuth"
import "js/Core.js" as Core

Page {
    id: root

    property string name: "LatestScenePage"
    property int per_page: 25
    property alias page_number: navigationBar.page
    property string filter: currentFilter().filterId
    property string category_name: currentFilter().category_name
    property string ext_info_type: currentFilter().ext_info_type

    tools: mainToolBar

    function currentFilter() {
        return filterModel.get(filterListView.currentIndex);
    }

    ListModel {
        id: filterModel
        ListElement { name: "No filter"; ext_info_type: ""; category_name: ""; filterId: 0; }
        ListElement { name: "xREL Standard-Filter"; ext_info_type: ""; category_name: ""; filterId: 6; }
        ListElement { name: "Deutsche Filme und Serien"; ext_info_type: ""; category_name: ""; filterId: 7; }

        ListElement { name: "Movies"; category_name: "movies"; ext_info_type: ""; filterId: 0; }
        ListElement { name: "Movies - Topmovies"; category_name: "topmovie"; ext_info_type: "movie"; filterId: 0; }
        ListElement { name: "Movies - Xvid"; category_name: "xvid"; ext_info_type: "movie"; filterId: 0; }
        ListElement { name: "Movies - DVDR"; category_name: "dvdr"; ext_info_type: "movie"; filterId: 0; }
        ListElement { name: "Movies - HDTV"; category_name: "hdtv"; ext_info_type: "movie"; filterId: 0; }
        ListElement { name: "Movies - x264"; category_name: "x264"; ext_info_type: "movie"; filterId: 0; }
        ListElement { name: "Movies - VCD"; category_name: "vcd"; ext_info_type: "movie"; filterId: 0; }
        ListElement { name: "Movies - MDVDR"; category_name: "mdvdr"; ext_info_type: "movie"; filterId: 0; }
        ListElement { name: "Movies - Retail"; category_name: "retail"; ext_info_type: "movie"; filterId: 0; }
        ListElement { name: "Movies - English"; category_name: "english"; ext_info_type: "movie"; filterId: 0; }
        ListElement { name: "Movies - 3D"; category_name: "3d"; ext_info_type: "movie"; filterId: 0; }
        ListElement { name: "Movies - XXX"; category_name: "xxx"; ext_info_type: "xxx"; filterId: 0; }

        ListElement { name: "Games"; category_name: "games"; ext_info_type: ""; filterId: 0; }
        ListElement { name: "Games - Updates"; category_name: "update"; ext_info_type: "game"; filterId: 0; }

        ListElement { name: "Console"; category_name: "console"; ext_info_type: "console"; filterId: 0; }
        ListElement { name: "Console - Playstation"; category_name: "playstation"; ext_info_type: "console"; filterId: 0; }
        ListElement { name: "Console - Xbox360"; category_name: "xbox360"; ext_info_type: "console"; filterId: 0; }
        ListElement { name: "Console - Wii"; category_name: "wii"; ext_info_type: "console"; filterId: 0; }
        ListElement { name: "Console - PSP"; category_name: "psp"; ext_info_type: "console"; filterId: 0; }
        ListElement { name: "Console - NDS"; category_name: "nds"; ext_info_type: "console"; filterId: 0; }
        ListElement { name: "Console - Wii Dl"; category_name: "wiidl"; ext_info_type: "console"; filterId: 0; }
        ListElement { name: "Console - Xbox360 DLC"; category_name: "x360dlc"; ext_info_type: "console"; filterId: 0; }
        ListElement { name: "Console - Xbox"; category_name: "xbox"; ext_info_type: "console"; filterId: 0; }
        ListElement { name: "Console - Gamecube"; category_name: "gamecube"; ext_info_type: "console"; filterId: 0; }

        ListElement { name: "TV"; category_name: "tv"; ext_info_type: "tv"; filterId: 0; }
        ListElement { name: "TV - Series"; category_name: "tvseries"; ext_info_type: "tv"; filterId: 0; }
        ListElement { name: "TV - Documentations"; category_name: "tvdoc"; ext_info_type: "tv"; filterId: 0; }
        ListElement { name: "TV - Shows"; category_name: "tvshow"; ext_info_type: "tv"; filterId: 0; }
        ListElement { name: "TV - HDTV"; category_name: "hdtv"; ext_info_type: "tv"; filterId: 0; }
        ListElement { name: "TV - English"; category_name: "english"; ext_info_type: "tv"; filterId: 0; }
        ListElement { name: "TV - Sport"; category_name: "tvsport"; ext_info_type: "tv"; filterId: 0; }
        ListElement { name: "TV - Retail"; category_name: "retail"; ext_info_type: "tv"; filterId: 0; }
        ListElement { name: "TV - Custom subbed"; category_name: "tvsubbed"; ext_info_type: "tv"; filterId: 0; }

        ListElement { name: "Software"; category_name: "apps"; ext_info_type: ""; filterId: 0; }

        ListElement { name: "Spezial - English"; category_name: "english"; ext_info_type: ""; filterId: 0; }
        ListElement { name: "Spezial - HDTV"; category_name: "hdtv"; ext_info_type: ""; filterId: 0; }
        ListElement { name: "Spezial - Internal"; category_name: "internal"; ext_info_type: ""; filterId: 0; }
        ListElement { name: "Spezial - Anime"; category_name: "anime"; ext_info_type: ""; filterId: 0; }
        ListElement { name: "Spezial - Retail"; category_name: "retail"; ext_info_type: ""; filterId: 0; }
    }

    XmlListModel {
        id: xmlCategoryModel
        source: "http://api.xrel.to/api/release/browse_category.xml?category_name=" + category_name + "&ext_info_type=" + ext_info_type
        query: "/releases/list/release"

        XmlRole { name: "id"; query: "id/string()" }
        XmlRole { name: "dirname"; query: "dirname/string()" }
        XmlRole { name: "link_href"; query: "link_href/string()" }
        XmlRole { name: "time"; query: "time/string()" }
        XmlRole { name: "group_name"; query: "group_name/string()" }
        XmlRole { name: "size_number"; query: "size/number/string()" }
        XmlRole { name: "size_unit"; query: "size/unit/string()" }
        XmlRole { name: "video_type"; query: "video_type/string()" }
        XmlRole { name: "audio_type"; query: "audio_type/string()" }
        XmlRole { name: "ext_info_type"; query: "ext_info/type/string()" }
        XmlRole { name: "ext_info_id"; query: "ext_info/id/string()" }
        XmlRole { name: "ext_info_title"; query: "ext_info/title/string()" }
        XmlRole { name: "ext_info_link_href"; query: "ext_info/link_href/string()" }
        XmlRole { name: "ext_info_rating"; query: "ext_info/rating/number()" }
        XmlRole { name: "tv_season"; query: "tv_season/string()" }
        XmlRole { name: "tv_episode"; query: "tv_episode/string()" }
        XmlRole { name: "flags_english"; query: "flags/english/string()" }
    }

    OAuthXmlListModel {
        id: xmlDisplayModel
        oauthSource: "http://api.xrel.to/api/release/latest.xml"
        query: "/releases/list/release"
        token: globalSettings.oauthToken
        secret: globalSettings.oauthSecret
        parameters: [ ["per_page", per_page], ["page", page_number], ["filter", filterId] ]
        oauthEnabled: globalSettings.loggedIn

        property string filterId: filter === 0 && globalSettings.loggedIn ? "overview" : filter

        XmlRole { name: "id"; query: "id/string()" }
        XmlRole { name: "dirname"; query: "dirname/string()" }
        XmlRole { name: "link_href"; query: "link_href/string()" }
        XmlRole { name: "time"; query: "time/string()" }
        XmlRole { name: "group_name"; query: "group_name/string()" }
        XmlRole { name: "size_number"; query: "size/number/string()" }
        XmlRole { name: "size_unit"; query: "size/unit/string()" }
        XmlRole { name: "video_type"; query: "video_type/string()" }
        XmlRole { name: "audio_type"; query: "audio_type/string()" }
        XmlRole { name: "ext_info_type"; query: "ext_info/type/string()" }
        XmlRole { name: "ext_info_id"; query: "ext_info/id/string()" }
        XmlRole { name: "ext_info_title"; query: "ext_info/title/string()" }
        XmlRole { name: "ext_info_link_href"; query: "ext_info/link_href/string()" }
        XmlRole { name: "ext_info_rating"; query: "ext_info/rating/number()" }
        XmlRole { name: "tv_season"; query: "tv_season/string()" }
        XmlRole { name: "tv_episode"; query: "tv_episode/string()" }
        XmlRole { name: "flags_english"; query: "flags/english/string()" }
    }

    PageBackground { }

    PageHeader {
        id: pageHeader
        anchors.top: parent.top
        text: qsTr("Scene releases")
    }

    ListView {
        id: filterListView
        anchors.top: pageHeader.bottom
        anchors.left: parent.left
        anchors.right: parent.right
        height: 40
        cacheBuffer: model.count*height
        snapMode: ListView.SnapToItem
        highlightRangeMode: ListView.StrictlyEnforceRange
        highlightMoveDuration: 1000
        clip: true

        model: filterModel
        delegate: FilterDelegate { }

        MouseArea {
            anchors.fill: parent
            onClicked: filterDialog.open();
        }
    }

    Separator {
        anchors.bottom: filterListView.bottom
    }

    ListView {
        id: listView
        anchors.top: filterListView.bottom
        anchors.left: parent.left
        anchors.right: parent.right
        anchors.bottom: navigationBar.top
        cacheBuffer: per_page*92
        spacing: 6
        clip: true
        delegate: ReleaseSceneDelegate { }
        model: xmlDisplayModel
        snapMode: ListView.SnapToItem

        XmlViewBusyIndicator { opacity: (xmlModel.status === XmlListModel.Loading) || xmlDisplayModel.oauthLoading ? 1.0 : 0.0 }

        onFlickEnded: navigationBar.show = atYEnd || atYBeginning

        function showReleaseSceneInfo(id) {
            pageStack.push(Qt.resolvedUrl("ReleaseSceneInfoPage.qml"), { id: id })
        }

        ScrollDecorator {
            flickableItem: listView
        }
    }

    NavigationBar {
        id: navigationBar
        onReload: xmlDisplayModel.reload();
    }

    SelectionDialog {
        id: filterDialog
        titleText: "Select a filter:"
        model: filterModel
        selectedIndex: filterListView.currentIndex

        onSelectedIndexChanged: {
            filterListView.currentIndex = selectedIndex;
            selectedIndex = filterListView.currentIndex;
        }
    }

    states: [
        State {
            name: "BROWSE_CATEGORY"
            when: category_name != ""
            PropertyChanges { target: navigationBar; enabled: false; }
            PropertyChanges { target: listView; model: xmlCategoryModel; }
        }
    ]
}
